﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMS_Server;
using SMS_Server_Manager;

namespace SMS_Server_Manager.Forms
{
    public partial class StaticResponse : Form
    {
        public StaticResponse()
        {
            InitializeComponent();
        }

        private Action_StaticResponse staticResponse;

        public Action_StaticResponse Static_Response
        {
            get { return staticResponse; }
            set
            {
                staticResponse = value;
                FunctionName.Text = staticResponse.ActionName;
                ParamFormat.Text = staticResponse.ActionFormat;
                ResponseText.Text = staticResponse.ResponseString;
            }
        }

        private Form Sender;

        public System.Windows.Forms.DialogResult ViewDialog(Form sender)
        {
            Sender = sender;
            return this.ShowDialog(sender);
        }

        private void CancleButton_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void ContinueButton_Click(object sender, EventArgs e)
        {
            if (!validators.IsValidString(ResponseText.Text))
            {
                MessageBox.Show("Invalid response text", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //lets save the staticResponse now
            staticResponse.ResponseString = ResponseText.Text;
            DataAccess DA = new DataAccess(Global.GetConfigurationDatabaseName(), Global.DatabasePassword);
            if (staticResponse.actionID == 0)
            {
                DA.CommandString = "INSERT INTO Action_Static (FunctionName, Format, Response) VALUES (@FunctionName, @Format, @Response);";
                DA.AddParameter("@FunctionName", staticResponse.ActionName);
                DA.AddParameter("@Format", staticResponse.ActionFormat);
                DA.AddParameter("@Response", staticResponse.ResponseString);
                DA.ExecuteNonQuery();
                DA.CommandString = "SELECT ActionID FROM Action_Static WHERE FunctionName=@FunctionName AND Format=@Format AND Response=@Response;";
                DA.ExecuteReader();
                while (DA.Reader.Read())
                {
                    int ActionID = (int)DA.Reader.GetInt64(0);
                    bool ActionExist = false;
                    foreach (Incoming_Action ia in ((Function)((Mainform)Sender).FunctionList.SelectedItem).Actions)
                    {
                        if (ia.ActionID() == (long)ActionID && ia.ActionType() == ActionTypes.StaticResponse)
                        {
                            ActionExist = true;
                        }
                    }
                    if (!ActionExist)
                    {
                        staticResponse.actionID = ActionID;
                        ((Function)((Mainform)Sender).FunctionList.SelectedItem).Actions.Add(staticResponse);
                        break;
                    }
                }
                DA.Reader.Close();
                DA.Close();
                ((Mainform)Sender).ActionList.Items.Add(staticResponse);
            }
            else
            {
                DA.CommandString = "UPDATE Action_Static SET Response=@Response WHERE ActionID=@ActionID;";
                DA.AddParameter("@Response", staticResponse.ResponseString);
                DA.AddParameter("@ActionID", staticResponse.actionID);
                DA.ExecuteNonQuery();
                DA.Close();
            }
            Global.unsaved_changes = true;
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

    }
}
